// Copyright 2023 The titan Authors. All rights reserved.

// Project: titan
// IDE: GoLand
// Author: fucaihe@outlook.com
// File: redis.go
// Date: 2023/12/18 15:29:18
// Description: Redis实现的分布式锁

package mutex

//import (
//	"github.com/go-redsync/redsync/v4"
//	"github.com/go-redsync/redsync/v4/redis/goredis/v9"
//	goredislib "github.com/redis/go-redis/v9"
//	"github.com/spf13/viper"
//)
//
//const defaultMutexName = "wf-global-mutex-deduplication"
//
//func NewClient(conf *viper.Viper) *goredislib.Client {
//	return goredislib.NewClient(&goredislib.Options{
//		Addr:     conf.GetString("data.redis.addr"),
//		Username: conf.GetString("data.redis.username"),
//		Password: conf.GetString("data.redis.password"),
//		DB:       conf.GetInt("data.redis.db"),
//	})
//}
//
//func NewRedsync(client *goredislib.Client) *redsync.Redsync {
//	pool := goredis.NewPool(client)
//	return redsync.New(pool)
//}
//
//// NewDefaultMutex 新建一个默认的分布式锁
//func NewDefaultMutex(rs *redsync.Redsync) *redsync.Mutex {
//	return rs.NewMutex(defaultMutexName)
//}

// NewDistributedMutex 新建一个分布式锁
//func NewDistributedMutex(conf *viper.Viper) {
//	client := goredislib.NewClient(&goredislib.Options{
//		Addr:     conf.GetString("data.redis.addr"),
//		Username: conf.GetString("data.redis.username"),
//		Password: conf.GetString("data.redis.password"),
//		DB:       10,
//	})
//	pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)
//
//	// Create an instance of redisync to be used to obtain a mutual exclusion lock.
//	rs := redsync.New(pool)
//
//	// Obtain a new mutex by using the same name for all instances wanting the same lock.
//	mutexname := "my-global-mutex"
//	mutex := rs.NewMutex(mutexname)
//
//	// Obtain a lock for our given mutex. After this is successful, no one else can obtain the same lock (the same mutex name) until we unlock it.
//	if err := mutex.Lock(); err != nil {
//		panic(err)
//	}
//
//	// Do your work that requires the lock.
//
//	// Release the lock so other processes or threads can obtain a lock.
//	if ok, err := mutex.Unlock(); !ok || err != nil {
//		panic("unlock failed")
//	}
//}
